package net.newsmth.smthapp.android.widget;

import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import net.newsmth.smthapp.android.R;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Hatsune Miku on 5/21/14.
 */
public class ArticleListBottomBar extends RelativeLayout implements View.OnClickListener {
    private static Logger logger = LoggerFactory.getLogger(ArticleListBottomBar.class);
    private boolean isInit = false;

    private int orignalWidth = 0;
    private int orignalLeft = 0;
    private boolean isExpand = true;

    private RelativeLayout pagerButtonContainer;
    private ImageView toogleImageView;

    public ArticleListBottomBar(Context context) {
        super(context);
        init(context);
    }

    public ArticleListBottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ArticleListBottomBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        View bottomBar = LayoutInflater.from(context).inflate(R.layout.view_article_list_bottom_bar, null);

        pagerButtonContainer = (RelativeLayout)bottomBar.findViewById(R.id.pager_buttons_container);
        toogleImageView = (ImageView)bottomBar.findViewById(R.id.toggle_button);
        toogleImageView.setClickable(true);
        toogleImageView.setOnClickListener(this);
        this.addView(bottomBar);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        if (!isInit && changed) {
            orignalWidth = pagerButtonContainer.getWidth();
            orignalLeft = pagerButtonContainer.getLeft();

            logger.debug("Width is {}", orignalWidth);
            logger.debug("Left is {}", orignalLeft);

            isInit = true;
        }
    }

    @Override
    public void onClick(View view) {
        if (view == toogleImageView) {

            if (isExpand) {
                hidePagerButtons();
                isExpand = false;
            } else {
                showPagerButtons();
                isExpand = true;
            }
        }
    }

    private void showPagerButtons() {
        TooglePagerButtonAnimation animation = new TooglePagerButtonAnimation(0, orignalWidth);
        animation.setDuration((int )(orignalWidth / pagerButtonContainer.getContext().getResources().getDisplayMetrics().density));
        pagerButtonContainer.startAnimation(animation);
    }

    private void hidePagerButtons() {
        TooglePagerButtonAnimation animation = new TooglePagerButtonAnimation(orignalWidth, 0);
        animation.setDuration((int )(orignalWidth / pagerButtonContainer.getContext().getResources().getDisplayMetrics().density));
        pagerButtonContainer.startAnimation(animation);
    }

    private class TooglePagerButtonAnimation extends Animation {
        private int orignalX = 0;
        private int targetX = 0;

        public TooglePagerButtonAnimation(int orignalX, int targetX) {
            this.orignalX = orignalX;
            this.targetX = targetX;
        }


        @Override
        public boolean willChangeBounds() {
            return true;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            LayoutParams params = (LayoutParams)pagerButtonContainer.getLayoutParams();
            params.width = orignalX +  (int)((targetX - orignalX) * interpolatedTime);
            pagerButtonContainer.setLayoutParams(params);
        }
    }
}
